package com.assets.lease;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.assets.Adapter.Input_Adapter_item1;
import com.assets.BaseActivity;
import com.assets.Ip_set.Url;
import com.assets.R;
import com.assets.bean.Input_item1;
import com.assets.bean.Issued_bean;
import com.assets.util.TrustAllCerts;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hb.dialog.dialog.LoadingDialog;

import org.apache.http.conn.ssl.AllowAllHostnameVerifier;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Lease_lv extends BaseActivity {
    private TextView person, money, return_date, time, loan_date;
    private ListView issued_lv;
    LoadingDialog loadingDialog;
    private ImageView fh;
    private List<Input_item1> data = new ArrayList<>();
    private String url = "http://" + Url.IP + "/api/leaseQueryById";
    private String leaseid, status;

    @Override
    public int intiLayout() {
        return R.layout.lease_lv;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadingDialog = new LoadingDialog(this);
        loadingDialog.setMessage("加载中....");
        loadingDialog.show();
    }

    @Override
    public void initView() {
        fh = (ImageView) findViewById(R.id.fh);
        person = (TextView) findViewById(R.id.person);
        money = (TextView) findViewById(R.id.money);
        return_date = (TextView) findViewById(R.id.return_date);
        time = (TextView) findViewById(R.id.time);
        loan_date = (TextView) findViewById(R.id.loan_date);
        issued_lv = (ListView) findViewById(R.id.issued_lv);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        person.setText(intent.getStringExtra("person"));
        money.setText(intent.getStringExtra("money"));
        return_date.setText(intent.getStringExtra("return_date"));
        time.setText(intent.getStringExtra("time"));
        loan_date.setText(intent.getStringExtra("loan_date"));
        leaseid = intent.getStringExtra("leaseid");
        status = intent.getStringExtra("status");
        load();
        fh.setOnClickListener(v -> finish());
    }

    private void load() {
        new Thread() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient
                            .Builder()
                            .hostnameVerifier(new AllowAllHostnameVerifier())
                            .sslSocketFactory(TrustAllCerts.createSSLSocketFactory(), TrustAllCerts.getX509TrustManager())
                            .connectTimeout(10, TimeUnit.SECONDS)//连接超时
                            .readTimeout(500, TimeUnit.SECONDS)
                            .build();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("leaseId", leaseid)
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody).build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    Log.i("222", responseData);
                    showResponse(responseData);//解析
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private void showResponse(final String responseData) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadingDialog.dismiss();
                if (responseData.equals("[]")) {
                    Toast.makeText(Lease_lv.this, "无数据", Toast.LENGTH_SHORT).show();
                    return;
                }
                Gson gson = new Gson();
                List<Issued_bean> userList = gson.fromJson(responseData, new TypeToken<List<Issued_bean>>() {
                }.getType());
                for (int i = 0; i < userList.size(); i++) {
                    String assetName = userList.get(i).getAssetName();
                    String deptId = userList.get(i).getDeptId();
                    String assetNumber = userList.get(i).getAssetNumber();
                    String userId = userList.get(i).getUserId();
                    String model = userList.get(i).getModel();
                    String type = userList.get(i).getPlace();
                    String image = "";
                    if (!TextUtils.isEmpty(userList.get(i).getImage())) {
                        image = "http://" + Url.IP + userList.get(i).getImage();
                    }
                    data.add(new Input_item1(image, assetName, deptId, assetNumber, userId, model, type, ""));
                }
                Input_Adapter_item1 adapter_item = new Input_Adapter_item1(Lease_lv.this, R.layout.input_adapter_item, data);
                issued_lv.setAdapter(adapter_item);
            }
        });
    }
}
